package com.phpxiu.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.phpxiu.app.adapter.holder.HomeHotViewHolder;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.LiveRoomHost;
import com.phpxiu.app.model.list.HomeHot;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.UserHome;
import com.phpxiu.app.view.custom.ListEmptyView;
import com.phpxiu.app.view.fragment.HomeHotFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by WuJinZhou on 2016/5/6.
 */
public class HomeHotListAdapter extends BaseAdapter<HomeHot> implements View.OnClickListener {
    public final static String TAG="HomeAttentionListAdapter";
    private HomeHot empty=new HomeHot();
    private ListEmptyView emptyView;
    private WeakReference<HomeHotFragment>callbackWeak;
    private LinearLayout.LayoutParams mPicBoxParams;
    private  int picBoxHeight=0;
    int bannerBoxHeight=0;
    public HomeHotListAdapter(Context context, List<HomeHot> data, int screenWidth) {
        super(context, data);
        empty.setListViewType(HomeHot.EMPTY_VIEW);
        data.add(0,empty);
        emptyView=new ListEmptyView(context);
        emptyView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setMsg(context.getString(R.string.list_empty_title));
        picBoxHeight=(screenWidth*732)/750;
        bannerBoxHeight=(screenWidth*251)/750;
        emptyView.setPadding(0,bannerBoxHeight,0,0);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomeHot a=data.get(position);
        if(a.getListViewType()== HomeHot.EMPTY_VIEW){
            return emptyView;
        }
        LiveRoomHost host=a.getHost();
        HomeHotViewHolder holder=null;
        if(convertView==null||convertView==emptyView){
            convertView=mInflater.inflate(R.layout.home_hot_list_item_view,null);
            holder=new HomeHotViewHolder(convertView);
            if(mPicBoxParams==null){
                mPicBoxParams=(LinearLayout.LayoutParams)holder.picBox.getLayoutParams();
                mPicBoxParams.height=picBoxHeight;
            }
            holder.picBox.setLayoutParams(mPicBoxParams);
            convertView.setTag(holder);
        }
        holder=(HomeHotViewHolder)convertView.getTag();
        try {
            String head=host.getAvatar();
            if(head!=null&&!"".equals(head)) {
                holder.head.setImageURI(Uri.parse(head));
            }
            holder.head.setTag(host.getUid());
            holder.head.setOnClickListener(this);
            holder.nick.setText(host.getNickname());
            holder.position.setText(a.getLbs().getAddress());
            holder.online.setText(a.getOnlineCount());
            String cover=a.getCover();
            if(cover==null||"".equals(cover)){
                holder.pic.setImageURI(Uri.parse("res:///"+R.drawable.cover_background));
            }else{
                holder.pic.setImageURI(Uri.parse(cover));
            }
            holder.pic.setTag(a);
            holder.pic.setOnClickListener(this);
            String t=a.getType();
            if("3".equals(t)){
                holder.stateSign.setImageResource(R.mipmap.live_type_pay_white_icon);
            }else if("2".equals(t)){
                holder.stateSign.setImageResource(R.mipmap.live_type_locked_white_icon);
            }else{
                holder.stateSign.setImageResource(R.mipmap.live_type_public_white_icon);
            }
            //此处应为话题或与微博相关
            if(a.getTitle()!=null&&!"".equals(a.getTitle())){
                if(holder.topic.getVisibility()!=View.VISIBLE){
                    holder.topic.setVisibility(View.VISIBLE);
                }
                holder.topic.setText(a.getTitle());
            }else{
                if(holder.topic.getVisibility()==View.VISIBLE){
                    holder.topic.setVisibility(View.GONE);
                }
            }
        }catch (NullPointerException e){
            KKYUtil.log(TAG, e.getMessage() + "");
            return null;
        }
        return convertView;
    }

    /**
     * 刷新列表
     */
    public void refresh() {
        if(emptyView!=null){
            emptyView.loaded();
        }
        if(data.size()>1){
            for (HomeHot a:data){
                if(a.getListViewType()== HomeHot.EMPTY_VIEW){
                    data.remove(a);
                    break;
                }
            }
        }else if(data.size()==0){
            data.add(empty);
        }
        super.notifyDataSetChanged();
    }

    /**
     * 监听进入房间
     */
    public void setOnEnterRoomCallback(HomeHotFragment callback){
        if(callback==null){
            if(callbackWeak!=null){
                callbackWeak.clear();
                callbackWeak=null;
            }
        }else{
            if(callbackWeak!=null){
                callbackWeak.clear();
            }
            callbackWeak=new WeakReference<HomeHotFragment>(callback);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.avatar_view:
                if (callbackWeak != null && callbackWeak.get() != null) {
                    if(v.getTag()!=null&&!"".equals(v.getTag())){
                        Intent home=new Intent(context, UserHome.class);
                        home.putExtra(UserHome.EXTRA_PARAM_KEY_UID,v.getTag().toString());
                        context.startActivity(home);
                    }
                }
                break;
            case R.id.pic_view:
                if (callbackWeak != null && callbackWeak.get() != null&&v.getTag()!=null&&v.getTag() instanceof HomeHot) {
                    callbackWeak.get().onEnterRoom((HomeHot) v.getTag());
                }
                break;
            default:
                break;
        }
    }
}
